package com.linjacy.httpproject.component;

import com.linjacy.httpproject.entity.SysLog;
import com.linjacy.httpproject.mapper.SysLogMapper;
import com.linjacy.httpproject.service.SysLogService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Component
public class LogQueueTaskHandler implements QueueTaskHandler{

    public String logParam;

    public String logResult;

    public String executeTime;

    @Resource
    SysLogMapper sysLogMapper;

    @Autowired
    QueueGenerationService queueGenerationService;

    public void setBaseParamAndAddLogTask(String logParam, String logResult, String executeTime){
        this.logParam = logParam;
        this.logResult = logResult;
        this.executeTime = executeTime;
        System.out.println("插入数据成功");
        queueGenerationService.addTask(this);
    }


    @Override
    public void processData() {
        System.out.println("执行异步日志任务");
        if(!StringUtils.isNullOrEmpty(logResult)){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SysLog sysLog = new SysLog();
            sysLog.setLogId(UUID.randomUUID().toString().replaceAll("\\-", ""));
            sysLog.setLogParam(logParam);
            sysLog.setLogResult(logResult);
            sysLog.setCreateTime(format.format(new Date()));
            sysLog.setExecuteTime(executeTime);
            sysLogMapper.insert(sysLog);
        }
    }

}
